package com.yanqu.road.dao.impl.union.honors;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.honors.UnionHonorsPointData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionHonorsPointDataDaoImpl extends TempDataObjectDao<UnionHonorsPointData> {

    public static String getUnionHonorsPointDataSql = "select * from `t_c_union_honor_point`";
    public Map<String, List<UnionHonorsPointData>> getUnionHonorsPointData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<UnionHonorsPointData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHonorsPointDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHonorsPointData data = parse(rs);
                if(!result.containsKey(data.getUnionUid())){
                    result.put(data.getUnionUid(), new ArrayList<>());
                }
                result.get(data.getUnionUid()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHonorsPointDataSql = "insert into `t_c_union_honor_point`(unionUid,activityType,activityChildType,honors,lastUpdateTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHonorsPointData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityChildType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHonors()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastUpdateTime()));
        if (getDb().executeNoneQuery(insertUnionHonorsPointDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHonorsPointDataSql = "update `t_c_union_honor_point` set honors=?,lastUpdateTime=? where unionUid=? and activityType=? and activityChildType=?";

    @Override
    protected boolean updateData(UnionHonorsPointData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getHonors()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityChildType()));
        if (getDb().executeNoneQuery(updateUnionHonorsPointDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHonorsPointData parseData(ResultSet rs) throws SQLException {
        UnionHonorsPointData data = new UnionHonorsPointData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setActivityType(rs.getInt("activityType"));
        data.setActivityChildType(rs.getInt("activityChildType"));
        data.setHonors(rs.getLong("honors"));
        data.setLastUpdateTime(rs.getInt("lastUpdateTime"));

        return data;
    }
}
